load("@io_bazel_rules_go//go:def.bzl", "go_binary", "go_cross_binary", "go_library", "go_test")
load("@io_bazel_rules_go//go/tools/bazel_testing:def.bzl", "go_bazel_test")
load(":def.bzl", "no_context_info")

test_suite(
    name = "cross",
)

go_binary(
    name = "windows_cross",
    srcs = ["main.go"],
    goarch = "amd64",
    goos = "windows",
    pure = "on",
    deps = [":platform_lib"],
)

go_binary(
    name = "linux_cross",
    srcs = ["main.go"],
    goarch = "amd64",
    goos = "linux",
    pure = "on",
    deps = [":platform_lib"],
)

go_binary(
    name = "darwin_cross",
    srcs = ["main.go"],
    goarch = "amd64",
    goos = "darwin",
    pure = "on",
    deps = [":platform_lib"],
)

go_binary(
    name = "asm_cross",
    srcs = [
        "asm.s",
        "main.go",
    ],
    goarch = "386",
    goos = "linux",
    deps = [":platform_lib"],
)

go_binary(
    name = "native_bin",
    srcs = ["main.go"],
    pure = "on",
    deps = [":platform_lib"],
)

go_cross_binary(
    name = "windows_go_cross",
    platform = "@io_bazel_rules_go//go/toolchain:windows_amd64",
    target = ":native_bin",
)

go_cross_binary(
    name = "linux_go_cross",
    platform = "@io_bazel_rules_go//go/toolchain:linux_amd64",
    target = ":native_bin",
)

go_cross_binary(
    name = "darwin_go_cross",
    platform = "@io_bazel_rules_go//go/toolchain:darwin_amd64",
    target = ":native_bin",
)

# Because pure = "on" on the underlying target, this doesn't actually need cgo (and won't try to use it).
# This target ensures that (from Bazel 6) we don't require a C++ toolchain if we're not actually going to use cgo.
go_cross_binary(
    name = "windows_go_cross_cgo",
    platform = "@io_bazel_rules_go//go/toolchain:windows_amd64_cgo",
    target = ":native_bin",
)

# Because pure = "on" on the underlying target, this doesn't actually need cgo (and won't try to use it).
# This target ensures that (from Bazel 6) we don't require a C++ toolchain if we're not actually going to use cgo.
go_cross_binary(
    name = "linux_go_cross_cgo",
    platform = "@io_bazel_rules_go//go/toolchain:linux_amd64_cgo",
    target = ":native_bin",
)

# Because pure = "on" on the underlying target, this doesn't actually need cgo (and won't try to use it).
# This target ensures that (from Bazel 6) we don't require a C++ toolchain if we're not actually going to use cgo.
go_cross_binary(
    name = "darwin_go_cross_cgo",
    platform = "@io_bazel_rules_go//go/toolchain:darwin_amd64_cgo",
    target = ":native_bin",
)

go_library(
    name = "platform_lib",
    srcs = select({
        "//go/platform:darwin": ["lib_darwin.go"],
        "//go/platform:linux": ["lib_linux.go"],
        "//go/platform:windows": ["lib_windows.go"],
    }),
    importpath = "github.com/bazelbuild/rules_go/tests/core/cross/platform_lib",
)

go_test(
    name = "cross_test",
    size = "small",
    srcs = ["cross_test.go"],
    args = [
        "-darwin",
        "$(location :darwin_cross)",
        "-linux",
        "$(location :linux_cross)",
        "-windows",
        "$(location :windows_cross)",
    ],
    data = [
        ":darwin_cross",
        ":linux_cross",
        ":windows_cross",
    ],
    rundir = ".",
    deps = ["//go/tools/bazel:go_default_library"],
)

go_test(
    name = "go_cross_binary_test",
    size = "small",
    srcs = ["cross_test.go"],
    args = [
        "-darwin",
        "$(location :darwin_go_cross)",
        "-linux",
        "$(location :linux_go_cross)",
        "-windows",
        "$(location :windows_go_cross)",
    ],
    data = [
        ":darwin_go_cross",
        ":linux_go_cross",
        ":windows_go_cross",
    ],
    rundir = ".",
    deps = ["//go/tools/bazel:go_default_library"],
)

go_bazel_test(
    name = "ios_select_test",
    srcs = ["ios_select_test.go"],
)

go_bazel_test(
    name = "proto_test",
    srcs = ["proto_test.go"],
)

go_bazel_test(
    name = "sdk_version_test",
    srcs = ["sdk_version_test.go"],
)

go_bazel_test(
    name = "non_executable_test",
    srcs = ["non_executable_test.go"],
)

no_context_info(
    name = "no_context_info",
)
